/**
 * 栈结构 先进后出
 * created by Five on 2018-11-7 18:16:55
 */

const CAPACITY = 10;

export default class Stack<T>{
    private _elements: Array<T>;
    private _size: number;

    constructor(capacity: number = CAPACITY) {
        this._elements = new Array<T>(capacity);
        this._size = 0;
    }

    public push(o: T) {
        if (o == this.peek())
            return;

        let len = this._elements.length;
        if (this._size >= len) {
            let temp = new Array<T>(len);
            this._elements = this._elements.concat(temp);
        }
        this._elements[this._size++] = o;
    }

    public pop(): T {
        return this._elements[--this._size];
    }

    public peek(): T {
        return this._elements[this._size - 1];
    }

    public size(): number {
        return this._size;
    }

    public empty(): boolean {
        return this._size == 0;
    }

    public clear(capacity: number = CAPACITY) {
        delete this._elements;
        this._elements = new Array<T>(capacity);
        this._size = 0;
    }
}